//app.js
App({
	onLaunch: function () {
	    var info = require('siteinfo.js');
	    info['resource'] = info.siteroot.replace('app/index.php', 'addons/junsion_wifinav/resource/');
	    this.siteInfo = info;
      wx.hideTabBar();
      wx.getStorage({
        key: 'appcolor',
        success: function(res) {
          setAppStyle(null, res.data)
        },
      })
	},
	onShow: function () {
	},
	onHide: function () {
	},
	onError: function (msg) {
		console.log(msg)
	},
	//加载微擎工具类
	util: require('we7/resource/js/util.js'),
  siteInfo: require('siteinfo.js'),
	getUserInfo : function (cb) {
		var openid = wx.getStorageSync('openid');
		var that = this;
		if (openid) {
			typeof cb == "function" && cb(openid);
		} else {
			//调用登录接口
			wx.login({
				success: function (res) {
					that.util.request({
						url: 'entry/wxapp/openid',
						data: { code: res.code, m: 'junsion_wifinav'},
						cachetime: 0,
						success: function (res) {
							wx.setStorageSync('openid', res.data);
							typeof cb == "function" && cb(res.data);
						}
					});
				},
			});
		}
	},
  delBoM: function (resp){
    let firstCode = resp.charCodeAt(0);
    if(firstCode < 0x20 || firstCode > 0x7f) {
      resp = resp.substring(1); // 去除第一个字符  
      return delBoM(resp);
    }
	else{
      return resp;
    }
  },
	getShareData: function(cb){
		var data = {m: 'junsion_wifinav'};
		if(arguments[1]==1) data.status = 1;
		else if(arguments[1]==2) data.status = 2;
		this.util.request({
			url: 'entry/wxapp/share',
			data: data,
			success: function (res) {
				cb(res);
			}
		});
	},
  playAudio(url, cb) {
    wx.getSystemInfo({
      success: function (res) {
        if (res.system.indexOf('iOS') >= 0) {
          wx.playBackgroundAudio({
            title: '录音',
            dataUrl: url,
            complete: function () {
              wx.onBackgroundAudioStop(function (res) {
                if (cb) cb();
              });
            }
          });
        } else {
          var m = wx.getBackgroundAudioManager()
          m.src = url;
          m.onStop(function (res) {
            if (cb) cb();
          });
          m.onEnded(function (res) {
            if (cb) cb();
          });
        }
      }
    })
  },
	getWiFiSto: function(that,cb){
		var time = wx.getStorageSync('timestamp');
		if(time){
			var timestamp=new Date().getTime();
			if(time - timestamp > 3600){
				wx.removeStorageSync('timestamp');
				wx.removeStorageSync('wifiList');
			}else{
				var wifiList = wx.getStorageSync('wifiList');
				that.setData({wifiList: wifiList});
				if(cb) cb(wifiList);
				return true;
			}
		}
		return false;
	},
	footerInit: function(that){
    wx.hideTabBar(); 
    wx.getStorage({
      key: 'appcolor',
      success: function (res) {
        setAppStyle(that, res.data)
      },
    })
		this.util.request({
			url: 'entry/wxapp/footer',
      data: { m: 'junsion_wifinav' },
			success: function (res) {
        if(res.data != -1){
          setAppStyle(that,res.data.appcolor)
          if (res.data.footer){
              var color = res.data.color;
              var pages = getCurrentPages();
              var currentPage = pages[pages.length - 1].route;
              that.setData({ footer: res.data.footer,color:color, currentPage: "/"+currentPage});
          }else wx.showTabBar(); 
        }
			}
		});
	},
});
function setAppStyle(that, color){
  if (!color) color = '#009aff';
  wx.setNavigationBarColor({
    frontColor: '#ffffff',
    backgroundColor: color,
  })
  if (that){
    wx.setStorage({
      key: 'appcolor',
      data: color,
    });
    that.setData({ 'appColor': color });
  }
}